﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows;

namespace AppToolkit.WPF.Services.UserMessageHandling.UI
{
    /// <summary>
    /// Converts severity infos (e.g. Info, Warning, Error, ...) to their corresponding bitmap representation.
    /// </summary>
    public class SeverityConverter : IValueConverter
    {
        const string Header = @"pack://application:,,,/AppToolkit.WPF;";
        private const string RelLocation = @"AppToolkit.WPF;AppToolkit.WPF/Icons/Messages";
        private const string ImgExt = "/{0}.png";
        readonly Dictionary<string, BitmapImage> _bitmaps = new Dictionary<string, BitmapImage>();

        /// <summary>
        /// CTOR
        /// </summary>
        public SeverityConverter()
        {
            var ids = new string[]
            {
                "Info","Warning","Error"
            };
            var fnames = new string[]
            {
                "Info","Warning","Error"
            };

            try
            {
                for (int i = 0; i < ids.Length; i++)
                {
                    _bitmaps.Add(ids[i], createBitmap(fnames[i]));
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError("Error during loading of icons:\r\n\r\n{0}", ex.ToString());
            }
        }

        private static BitmapImage createBitmap(string fname)
        {
            var bi = new BitmapImage();
            bi.BeginInit();
            // new Uri(@"pack://application:,,/sampleImages/watermelon.jpg");
            string format = string.Format(RelLocation + ImgExt, fname);
            bi.UriSource = new Uri(format, UriKind.Relative);
            bi.EndInit();
            return bi;
        }

        #region IValueConverter Members

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!targetType.IsAssignableFrom(typeof(BitmapImage)) || !(value is MessageType))
                return DependencyProperty.UnsetValue;

            BitmapImage img;
            var msgType = (MessageType)value;

            if (!Bitmaps.TryGetValue(Enum.GetName(typeof(MessageType), msgType), out img))
            {
                return DependencyProperty.UnsetValue;
            }

            return img;
        }

        /// <summary>
        /// Converting back is currently not supported.
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// Exposes the loaded bitmaps for testing purposes.
        /// </summary>
        public IDictionary<string, BitmapImage> Bitmaps
        {
            get
            {
                return _bitmaps;
            }
        }
    }
}